<?php
namespace Admin\Controller;
use Admin\Model\UserModel;
use Think\Controller;
class SignController extends Controller {
	public $_APPID ='';
	public $_AppSecret ='';
	
	 public function __construct(){
    	parent::__construct();
	 	$this->_APPID = C('APPID');
	 	$this->_AppSecret = C('AppSecret');
	 }
	
	
	public function inAction(){
		$userModel = new UserModel();  
		//用户授权登陆获取基本信息
		$userid =I('userid');
		$code = I('get.code');  
    	$state = I('get.state'); 
		$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->_APPID.'&secret='.$this->_AppSecret.'&code='.$code.'&grant_type=authorization_code';  
		
		$ch = curl_init();  
		curl_setopt($ch,CURLOPT_URL,$get_token_url);  
		curl_setopt($ch,CURLOPT_HEADER,0);  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );  
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);  
		$res = curl_exec($ch);  
		curl_close($ch);  
		$json_obj = json_decode($res,true);  
		
		//根据openid和access_token查询用户信息  
		$access_token = $json_obj['access_token'];  
		$openid = $json_obj['openid'];  
		if ($state =='main') {
			$dataUser['openid'] = $openid;
		    $dataUser['userauth'] = array('in','3,6,7,8');
			$userItem = $userModel->getUserItem($dataUser);
			if (!empty($userItem)) {
				$sign_cookie['sessionid']=$userItem['sessionid'];
				$this->loginsetcookie($sign_cookie,60*60*24*7, '/');
				$url = C('DOMAIN').U('Merchant/account'); 
				header("Location:".$url);  
			}else {
				$url = C('DOMAIN').U('Merchant/loginView'); 
				header("Location:".$url); 
			}
		}
		if($state=="code"){
		    $dataUser['openid'] = $openid;
		    $dataUser['userauth'] = array('in','3,6,7,8');
		    $userItem = $userModel->getUserItem($dataUser);
		    if (!empty($userItem)) {
		        $sign_cookie['sessionid']=$userItem['sessionid'];
		        $this->loginsetcookie($sign_cookie,60*60*24*7, '/');
		        $url = C('DOMAIN').U('Qrcode/showSpreadCode');
		        header("Location:".$url);
		    }else {
		        $url = C('DOMAIN').U('Merchant/loginView');
		        header("Location:".$url);
		    } 
		}
		
		if ($state=="shouyin") {
		    $dataUser['openid'] = $openid;
		    $dataUser['userauth'] = 3;
		    $userItem = $userModel->getUserItem($dataUser);
			if (!empty($userItem)) {
				$sign_cookie['sessionid']=$userItem['sessionid'];
		        $this->loginsetcookie($sign_cookie,60*60*24*7, '/');
				$url = C('DOMAIN').U('Qrcode/showBossCode');
				header("Location:".$url);
			}else {
		        $url = C('DOMAIN').U('Merchant/loginView');
		        header("Location:".$url);
	   		}
		}
		
		
		
		
		if ($state =='bind') {
			//绑定微信号
			$whereUser['userid'] = $userid;
			$dataUser['openid'] = $openid;
			$dataUser['sessionid'] = md5(uniqid(rand(), TRUE));
			$userResult = $userModel->UpUser($whereUser, $dataUser);
			
			$sign_cookie['sessionid']=$dataUser['sessionid'];
			$this->loginsetcookie($sign_cookie,60*60*24*7, '/');
			$url = C('DOMAIN').U('Merchant/account'); 
			header("Location:".$url);  
		}
		
		
		
	}
	
	public function outAction(){
		$userModel = new UserModel(); 
		$where['sessionid']=$_COOKIE['sessionid'];
		$userItem =$userModel->getUserItem($where);
		
		$userData['userid'] =$userItem['userid'];
		$userData['sessionid']=md5(uniqid(rand(), TRUE));
	    $outResult = $userModel->saveUser($userData);
	    
		$sign_cookie['sessionid'] = '';
        $this->loginsetcookie($sign_cookie, -1, '/');
        $this->redirect('Index/index');
	}
	/**
	 * cookie设置
	 * @param $var 设置的cookie名
	 * @param $value 设置的cookie值
	 * @param $life 设置的过期时间：为整型，单位秒 如60表示60秒后过期
	 * @param $path 设置的cookie作用路径
	 * @param $domain 设置的cookie作用域名
	 */
	public function loginsetcookie($array, $life=0, $path = '/') {
		$_cookName_ary = array_keys($array);
		for($i = 0; $i < count($array); $i++) {
			//echo $_cookName_ary[$i].'='.$array[$_cookName_ary[$i]].'<br>';
			$httpOnly = false;
			setcookie($_cookName_ary[$i], $array[$_cookName_ary[$i]], $life?(time()+$life):0, $path);
		}
	}
    
}